package fileSelection;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private FileSelector fileSelector;

	private static final long serialVersionUID = 1L;
	private JPanel mainPane;
	private JTextField txtDirectory;
	private JTextField txtFilenameInclude;
	private JTextField txtFilenameExclude;
	private JTextField txtFilenameResult;
	private JCheckBox chckbxIncludeSubfolders;
	private JCheckBox chckbxOverwrite;
	private JButton btnProcess;

	private JMenu mnMode;
	private JMenu mnHelp;

	private JScrollPane scrollPane;
	// panel for selecting files
	private JPanel filesPanel;
	// panel for displaying text
	private JTextArea fileDisplayTextArea;

	private JPanel fileDisplayPanel;
	private JCheckBox[] chbFiles;
	private JLabel lblStatus;

	/**
	 * Create the frame.
	 */
	public MainFrame(FileSelector fileSelector) {
		this.fileSelector = fileSelector;
		setTitle("Mittelung");
		setResizable(false);
		setBounds(100, 100, 734, 470);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnMode = new JMenu("Modus");
		menuBar.add(mnMode);

		mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);
		JMenuItem newItem = new JMenuItem("Version");
		newItem.setActionCommand("AC_COMM_VERSIONINFO");
		newItem.addActionListener(fileSelector);
		mnHelp.add(newItem);

		mainPane = new JPanel();
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(mainPane);
		mainPane.setLayout(null);

		JPanel optionsPanel = new JPanel();
		optionsPanel.setBounds(563, 5, 150, 400);
		mainPane.add(optionsPanel);
		optionsPanel.setLayout(null);

		JButton btnChangeDirectory = new JButton("Change directory");
		btnChangeDirectory.setBounds(0, 22, 150, 28);
		btnChangeDirectory.addActionListener(fileSelector);
		btnChangeDirectory.setActionCommand("btn_change_dir");
		optionsPanel.add(btnChangeDirectory);

		txtDirectory = new JTextField();
		txtDirectory.setToolTipText("Current Directory");
		txtDirectory.setEditable(false);
		txtDirectory.setBounds(0, 61, 150, 28);
		optionsPanel.add(txtDirectory);
		txtDirectory.setColumns(10);

		txtFilenameInclude = new JTextField();
		txtFilenameInclude.setText("_result.txt");
		txtFilenameInclude.setBounds(0, 153, 150, 20);
		optionsPanel.add(txtFilenameInclude);
		txtFilenameInclude.setColumns(10);

		JSeparator separator = new JSeparator();
		separator.setBounds(0, 100, 150, 10);
		optionsPanel.add(separator);

		JLabel lblNewLabel = new JLabel("Filename");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 106, 130, 20);
		optionsPanel.add(lblNewLabel);

		JLabel lblExclude = new JLabel("Excluding");
		lblExclude.setHorizontalAlignment(SwingConstants.CENTER);
		lblExclude.setBounds(0, 176, 150, 20);
		optionsPanel.add(lblExclude);

		txtFilenameExclude = new JTextField();
		txtFilenameExclude.setColumns(10);
		txtFilenameExclude.setBounds(0, 197, 150, 20);
		optionsPanel.add(txtFilenameExclude);

		JButton btnSearchFiles = new JButton("Search files");
		btnSearchFiles.setToolTipText("Search for Files in the given Folder.");
		btnSearchFiles.setActionCommand("btn_search_files");
		btnSearchFiles.addActionListener(fileSelector);
		btnSearchFiles.setBounds(12, 249, 128, 28);
		optionsPanel.add(btnSearchFiles);

		btnProcess = new JButton("Process");
		btnProcess.setToolTipText("Process selected Files");
		btnProcess.setActionCommand("btn_process");
		btnProcess.addActionListener(fileSelector);
		btnProcess.setBounds(10, 358, 128, 42);
		optionsPanel.add(btnProcess);

		JLabel lblInclude = new JLabel("Including");
		lblInclude.setHorizontalAlignment(SwingConstants.CENTER);
		lblInclude.setBounds(10, 137, 130, 14);
		optionsPanel.add(lblInclude);

		JLabel lblDirectory = new JLabel("Directory");
		lblDirectory.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblDirectory.setHorizontalAlignment(SwingConstants.CENTER);
		lblDirectory.setBounds(0, 0, 150, 20);
		optionsPanel.add(lblDirectory);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(0, 283, 150, 10);
		optionsPanel.add(separator_1);

		JLabel lblAusgabedatei = new JLabel("Outputfile");
		lblAusgabedatei.setHorizontalAlignment(SwingConstants.CENTER);
		lblAusgabedatei.setBounds(0, 288, 150, 20);
		optionsPanel.add(lblAusgabedatei);

		txtFilenameResult = new JTextField();
		txtFilenameResult.setText("Result.txt");
		txtFilenameResult.setBounds(0, 307, 150, 20);
		optionsPanel.add(txtFilenameResult);
		txtFilenameResult.setColumns(10);

		chckbxIncludeSubfolders = new JCheckBox("Include subfolders");
		chckbxIncludeSubfolders.setHorizontalAlignment(SwingConstants.CENTER);
		chckbxIncludeSubfolders.setFont(new Font("Tahoma", Font.PLAIN, 11));
		chckbxIncludeSubfolders.setBounds(0, 219, 150, 23);
		optionsPanel.add(chckbxIncludeSubfolders);

		chckbxOverwrite = new JCheckBox("overwrite result");
		chckbxOverwrite.setToolTipText("If the output file already exists, overwrite it without asking.");
		chckbxOverwrite.setSelected(true);
		chckbxOverwrite.setBounds(0, 328, 150, 23);
		optionsPanel.add(chckbxOverwrite);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 5, 539, 373);
		mainPane.add(scrollPane);

		filesPanel = new JPanel();
		filesPanel.setBounds(0, 0, 0, 60);
		filesPanel.setVisible(true);
		scrollPane.setViewportView(filesPanel);

		fileDisplayPanel = new JPanel();
		fileDisplayPanel.setBounds(0, 0, 0, 60);
		fileDisplayPanel.setVisible(false);

		fileDisplayTextArea = new JTextArea("Hallo");
		fileDisplayTextArea.setFont(new Font("Courier New", Font.PLAIN, 12));
		fileDisplayTextArea.setBounds(0, 0, 490, 373);
		// fileDisplayTextArea.setPreferredSize(new Dimension(490, 373));

		fileDisplayTextArea.setEditable(false);
		fileDisplayTextArea.setBackground(new Color(237, 237, 237));
		fileDisplayPanel.add(fileDisplayTextArea);

		scrollPane.setViewportView(filesPanel);
		filesPanel.setLayout(null);

		lblStatus = new JLabel("");
		lblStatus.setBounds(10, 389, 539, 16);
		mainPane.add(lblStatus);
		this.validate();
		this.repaint();
	}

	public void changeDirectory(String string) {
		this.txtDirectory.setText(string);
	}

	public void displayNewFiles(File[] matchingFiles) {
		filesPanel.removeAll();
		// switch to correct display mode
		filesPanel.setVisible(true);
		scrollPane.setViewportView(filesPanel);
		fileDisplayPanel.setVisible(false);
		filesPanel.revalidate();

		this.chbFiles = new JCheckBox[matchingFiles.length];

		int maxBoxLength = 0;
		for (int i = 0; i < this.chbFiles.length; i++) {
			chbFiles[i] = new JCheckBox(matchingFiles[i].getName());
			chbFiles[i].setHorizontalAlignment(SwingConstants.LEFT);
			chbFiles[i].setVisible(true);
			// by default, activate all
			chbFiles[i].doClick();
			chbFiles[i].setBounds(0, i * 20, 800, 25);
			filesPanel.add(chbFiles[i]);
			if (maxBoxLength < chbFiles[i].getWidth()) {
				maxBoxLength = chbFiles[i].getWidth();
			}
		}
		filesPanel.setPreferredSize(new Dimension(maxBoxLength, this.chbFiles.length * 20));
		filesPanel.repaint();
		lblStatus.setText("" + chbFiles.length + " files found.");
	}

	public void displayProcessorOptions(JPanel processorPanel) {
		filesPanel.removeAll();
		filesPanel.add(processorPanel);
		processorPanel.setVisible(true);
		filesPanel.setVisible(true);
		scrollPane.setViewportView(filesPanel);
		fileDisplayPanel.setVisible(false);
		filesPanel.revalidate();
	}

	public boolean[] checkCheckBoxes() {
		if (chbFiles == null) {
			return null;
		}
		boolean[] checked = new boolean[chbFiles.length];

		for (int i = 0; i < checked.length; i++) {
			checked[i] = chbFiles[i].isSelected();
		}

		return checked;
	}

	public void setStatus(String string) {
		lblStatus.setText(string);
	}

	/**
	 * Sets the Mode of the Display according to a new Mode
	 * 
	 * @param processButton
	 *            Changes the description of the Process-Button. If null,
	 *            "Process" is displayed
	 * @param excludeFilename
	 *            Changes the textField for excludes. If null, nothing changes
	 * @param includeFilename
	 *            Changes the textField for inlcudes. If null, nothing changes
	 * @param windowName
	 *            Changes the Title of the window
	 */
	public void setMode(String resultName, String processButton, String excludeFilename, String includeFilename,
			String windowName) {
		if (excludeFilename != null) {
			txtFilenameExclude.setText(excludeFilename);
		}
		if (includeFilename != null) {
			txtFilenameInclude.setText(includeFilename);
		}
		if (processButton != null) {
			btnProcess.setText(processButton);
		} else {
			btnProcess.setText("Process.");
		}
		if (resultName != null) {
			txtFilenameResult.setText(resultName);
		} else {
			txtFilenameResult.setText("Result.txt");
		}
		setTitle(windowName);
	}

	public void showFile(File file) {
		filesPanel.setVisible(false);
		fileDisplayPanel.setVisible(true);
		scrollPane.setViewportView(fileDisplayPanel);
		String fileContent = "";
		fileContent += file.getAbsolutePath() + "\n\n";
		try {
			String newLine = "";
			BufferedReader br = new BufferedReader(new FileReader(file));
			while (newLine != null) {
				fileContent = fileContent + newLine + "\n";
				newLine = br.readLine();
			}
			br.close();
		} catch (IOException e) {

		}

		fileDisplayTextArea.setLineWrap(false);
		fileDisplayTextArea.setWrapStyleWord(false);
		fileDisplayTextArea.setText(fileContent);
	}

	public void showInfo(String infoText) {
		filesPanel.setVisible(false);
		fileDisplayPanel.setVisible(true);
		scrollPane.setViewportView(fileDisplayPanel);
		fileDisplayTextArea.setLineWrap(true);
		fileDisplayTextArea.setWrapStyleWord(true);
		fileDisplayTextArea.setText(infoText);
		// not quite sure why this is necessary...however, if bounds are not set
		// here the info is not always correctly displayed.
		fileDisplayTextArea.setBounds(0, 0, 490, 373);
	}

	public String getFileNameInclude() {
		return txtFilenameInclude.getText();
	}

	public String getFileNameExclude() {
		return txtFilenameExclude.getText();
	}

	public String getFileNameResult() {
		return txtFilenameResult.getText();
	}

	public boolean getIncludeSubfolders() {
		return chckbxIncludeSubfolders.isSelected();
	}

	public boolean getOverwrite() {
		return chckbxOverwrite.isSelected();
	}

	protected void setFileNameResult(String result) {
		txtFilenameResult.setText(result);
	}

	protected void setFilenameInclude(String include) {
		txtFilenameInclude.setText(include);
	}

	protected void setFilenameExclude(String exclude) {
		txtFilenameExclude.setText(exclude);
	}

	protected void setOverwrite(boolean sel) {
		System.out.println(sel);
		chckbxOverwrite.setSelected(sel);
	}

	protected void setIncludeSubfolders(boolean incl) {
		System.out.println(incl);
		chckbxIncludeSubfolders.setSelected(incl);
	}

	public void addMode(String label, int id) {
		JMenuItem newItem = new JMenuItem(label);
		newItem.setActionCommand("AC_COMM_MODE" + id);
		newItem.addActionListener(fileSelector);
		mnMode.add(newItem);
		mnMode.repaint();
	}
}
